<?php
namespace Home\Controller;
use Common\Controller\HomebaseController;
/*
*  分类 导航 
*/
class CategoryController extends HomebaseController 
{
    protected $typeObj;
    protected $articleObj;
    public function _initialize()
    {
        parent::_initialize();
        $this->typeObj = M("Type");
        $this->articleObj = D("Content/article");
    }
    public function index()
    {

        $typeParam=$_GET['type'];
        if (is_numeric($typeParam)) {
            $where['id']=$typeParam;
        }else{
            $where['url']=$typeParam;
        }
        $type=$this->typeObj->field('id,redirect')->where($where)->find();
        $typeId=$type['id'];
        $redirect = unserialize($type["redirect"]); 
        $rtype=$redirect['rtype']; 
        $rid=$redirect['rid'];
        switch ($rtype) {
            case 'list':
                $this->rList($rid);
                break;
            case 'article':
                $this->rArticle($rid);
                break;
            case 'type':
                $this->rType($rid);
                break;
            case 'page':
                $this->rPage($rid,$typeId);
                break;            
            default:
                break;
        }
    }
    public function rList($rid)
    {

        $condition['status']=$this->status;
        $rtype=$this->typeObj->field('id,name,templatelist')->find($rid);
        $typeObjs= R("Content/obj/getChildTypeByParentId",array($rid));
        $typeIds="";
        $typeIdsArr=array();
        foreach ($typeObjs as $key => $typeObj) {
            $typeIdsArr[]=$typeObj['id'];
        }
        $typeIds=implode(',', $typeIdsArr);
        $condition['typeid']=array('in',$typeIds);
        $count      =  $this->articleObj->where($condition)->count();
        $Page       = new \Think\Page($count,12);
        $show       = $Page->homeShow();
        $list = $this->articleObj->relation("type")->where($condition)->order(array('top'=>'desc','createtime'=>'asc'))->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('count',$count);
        $this->assign('list',$list);
        $this->assign('page',$show); 
        $tpl=$rtype['templatelist'];
        $this->assign('type',$rtype);
        $this->display($this->lang.':list:'.$tpl);
    }

    public function rArticle($rid)
    {
        $articleObj = $this->articleObj->relation(true)->find($rid);
        if(!empty($articleObj)){
            $rtype=$articleObj['type'];
            $this->assign('article',$articleObj);
            $this->assign('type',$rtype);
            $tpl=$articleObj['template'];
            $this->display($this->lang.':article:'.$tpl);
        }else{
           $this->display("404");
        }
    }

    public function rType($typeId=0)
    {
        if ($typeId>0) {
            $type=$this->typeObj->field('id,redirect')->find($typeId);
            $redirect = unserialize($type["redirect"]); 
            $rtype=$redirect['rtype']; 
            $rid=$redirect['rid'];
            switch ($rtype) {
                case 'list':
                    $this->rList($rid);
                    break;
                case 'article':
                    $this->rArticle($rid);
                    break;
                case 'type':
                    $this->rType($rid);
                    break;
                case 'page':
                    $this->rPage($rid,$typeId);
                    break;            
                default:
                    break;
            }
        }else{
            $this->redirect('404');
        }
    }

    public function rPage($tpl=0,$typeId)
    {
        $rtype=$this->typeObj->field('id,name')->find($typeId);

        $this->assign('type',$rtype);
        $this->display($this->lang.':page:'.$tpl);
    }
}
?>